import 'dart:async';

import 'package:flutter/material.dart';

import '../common/flatPanel.dart';
import '../models/topicItem.dart';

class MegaSiteDialog extends StatefulWidget {
  MegaSiteDialog({Key key, this.item}) : super(key: key);

  final TopicItem item;

  @override
  _MegaSiteDialogState createState() => _MegaSiteDialogState();
}

class _MegaSiteDialogState extends State<MegaSiteDialog> {
  final _topic = TextEditingController();
  final _label = TextEditingController();
  final _detail = TextEditingController();
  final _addition = TextEditingController();
  final GlobalKey _formKey = new GlobalKey<FormState>();

  void _onSubmit() {
    var item = TopicItem(
      id: 0,
      topic: _topic.text,
      label: _label.text,
      detail: _detail.text,
      addition: _addition.text,
    );
    Navigator.pop(context, item);
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    if (widget.item.id != 0) {
      _topic.text = widget.item.topic;
      _label.text = widget.item.label;
      _detail.text = widget.item.detail;
      _addition.text = widget.item.addition;
    } else {
      if (widget.item.topic.isNotEmpty) _topic.text = widget.item.topic;
    }
  }

  @override
  Widget build(BuildContext context) {
    return FlatPanel(
      child: Form(
        key: _formKey,
        child: Column(
          children: <Widget>[
            TextFormField(
              controller: _topic,
              decoration: InputDecoration(
                hintText: '主题',
                prefixIcon: Icon(Icons.title),
              ),
              validator: (v) => v.trim().length > 0 ? null : '主题不能为空',
            ),
            TextFormField(
              controller: _label,
              decoration: InputDecoration(
                hintText: '名称',
                prefixIcon: Icon(Icons.label_important),
              ),
              validator: (v) => v.trim().length > 0 ? null : '名称不能为空',
            ),
            TextFormField(
              controller: _detail,
              decoration: InputDecoration(
                hintText: '详述',
                prefixIcon: Icon(Icons.content_paste),
              ),
              validator: (v) => v.trim().length > 0 ? null : '详述不能为空',
            ),
            TextFormField(
              controller: _addition,
              decoration: InputDecoration(
                hintText: '附注',
                prefixIcon: Icon(Icons.tag),
              ),
            ),
            Row(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Expanded(
                  flex: 1,
                  child: FlatButton(
                      child: Text('清空', style: TextStyle(fontSize: 24)),
                      onPressed: () {
                        (_formKey.currentState as FormState).reset();
                      }),
                ),
                Expanded(
                  flex: 1,
                  child: FlatButton(
                    child: Text('提交',
                        style: TextStyle(fontSize: 24, color: Colors.blue)),
                    onPressed: () {
                      if ((_formKey.currentState as FormState).validate())
                        _onSubmit();
                    },
                  ),
                ),
              ],
            )
          ],
        ),
      ),
    );
  }
}

Future<TopicItem> showItemAddDialog(BuildContext context,
    {TopicItem item}) async {
  return await showDialog<TopicItem>(
      context: context,
      builder: (context) {
        return Dialog(child: MegaSiteDialog(item: item));
      });
}
